package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.AdminRoleStaff;
import com.dingreading.cloud.admin.entity.HeadStaff;
import com.dingreading.cloud.admin.service.AdminRoleStaffService;
import com.dingreading.cloud.admin.service.HeadStaffService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/adminRoleStaff")
@Api(tags = "总部-总部角色关联总部员工管理")
public class AdminRoleStaffController {

    @Resource
    private AdminRoleStaffService roleStaffService;
    @Resource
    private HeadStaffService headStaffService;


    @ApiOperation(value = "根据角色获取用户列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId
    ) {
        Page<AdminRoleStaff> page = this.roleStaffService.pageList(pageUtil, roleId);

        List<AdminRoleStaff> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> uids = list.stream().map(AdminRoleStaff::getHeadStaffUid).collect(Collectors.toList());
            List<HeadStaff> headStaffList = headStaffService.getByUids(uids);
            Map<String, String> map = new HashMap<>();
            if (CollectionUtils.isNotEmpty(headStaffList)) {
                map = headStaffList.stream().collect(Collectors.toMap(HeadStaff::getUid, HeadStaff::getUserName));
            }
            for (AdminRoleStaff staff : list) {
                staff.setName(map.get(staff.getHeadStaffUid()));
            }
        }

        PageBeanDto dto = new PageBeanDto();
        dto.setList(page.getRecords());
        dto.setTotal(page.getTotalRow());
        return R.ok(dto);
    }


    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加用户到角色功能")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "uid") String uid
    ) {
        List<AdminRoleStaff> roleStaffList = roleStaffService.listByHeadStaffUid(uid);
        if (CollectionUtils.isNotEmpty(roleStaffList)) {
            for (AdminRoleStaff roleStaff : roleStaffList) {
                if (roleStaff.getRoleId().equals(roleId)) {
                    return R.fail("已加入该角色，不能操作");
                }
            }
        }

        AdminRoleStaff AdminRoleStaff = new AdminRoleStaff();
        AdminRoleStaff.setRoleId(roleId);
        AdminRoleStaff.setHeadStaffUid(uid);
        roleStaffService.saveOrUpdate(AdminRoleStaff);

        return R.ok();
    }

    @ApiOperation(value = "删除用户的角色功能")
    @DeleteMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "角色id") @RequestParam(value = "roleId") Long roleId,
            @ApiParam(value = "总部员工id") @RequestParam(value = "uid") String uid
    ) {
        int l = roleStaffService.dropBean(roleId, uid);
        if (l <= 0) return R.fail("删除失败");

        return R.ok();
    }

}
